package jwanalytics.web;

import javax.naming.InitialContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jwanalytics.ejb.dataobjects.JWAPageHitEvent;
import jwanalytics.ejb.webhouse.WebSessionAnalyticsLogger;
import jwanalytics.ejb.webhouse.WebSessionAnalyticsLoggerLocal;
import jwanalytics.web.utils.HttpUtils;

/** Holds all user data in session, including EJB references. */
public class UserSessionBean {
	
	public final String RETURNING_BROWSER_COOKIE_NAME = "returningBrowserId";
	
	public final String SESSION_COOKIE_NAME = "sessionCookieId";	
	
	public Long customerId;
	
	Long sessionId = null;	
	
	Long returningBrowserId = null;
	
	/** This logs all pageHits for the session. */
	WebSessionAnalyticsLoggerLocal webLogger = null;	
		
	public UserSessionBean() {
		try {
			InitialContext ctx = new InitialContext();
			final Object object2 = ctx.lookup(WebSessionAnalyticsLogger.LocalJNDIName);          		
			webLogger = (WebSessionAnalyticsLoggerLocal)object2;
		} catch (Throwable t) {
			throw new RuntimeException("Unable to get initial context", t);
		}
	}
	
	
	public Long getCustomerId() {
		return customerId;
	}
	
	
	public void setCustomerId(Long customerId) {
		this.customerId = customerId;
	}
	
	public void closeSession() {
		webLogger.closeSession();
	}
	
	/** THis will log a page hit for a particular page. */
	public void logPageHit(String pageName, HttpServletRequest request, HttpServletResponse response) {
		//@TODO: obtain browser and session id from cookies, if not present, set them on the response object and pass them to webLogger.logPageHit
		
		if (sessionId == null) {
			Cookie sessionCookie = HttpUtils.searchCookieFromRequest(request, SESSION_COOKIE_NAME);
			String sessionCookieValue = null;
			if (sessionCookie != null) {
				sessionCookie.getValue();
			}
			
			if (sessionCookieValue != null && !sessionCookieValue.trim().equals("")) {
				try {
					sessionId = Long.parseLong(sessionCookie.getValue());
				} catch(Throwable t) {
					sessionId = null;				
				}
			}
		}	
		

		if (returningBrowserId == null) {
			Cookie returningBrowserCookie = HttpUtils.searchCookieFromRequest(request, RETURNING_BROWSER_COOKIE_NAME);
			String returningBrowserCookieValue = null;
			if (returningBrowserCookie != null) {
				returningBrowserCookieValue = returningBrowserCookie.getValue();
			}
			
			if (returningBrowserCookieValue != null && !returningBrowserCookieValue.trim().equals("")) {
				try {
					returningBrowserId = Long.parseLong(returningBrowserCookie.getValue());
				} catch(Throwable t) {
					returningBrowserId = null;
				}
			}
		}
		
		JWAPageHitEvent pageHit = new JWAPageHitEvent(pageName, returningBrowserId, sessionId, request);
		webLogger.logPageHit(pageHit, true); //true for synchronous recording, will increase latency, decrease response time of web request a little.
		
		if (sessionId == null && webLogger.getSessionId() != null) {
			//HttpUtils.addCookie(SESSION_COOKIE_NAME, ""+webLogger.getSessionId(), response); no need to add this cookie as sessions are managed by Java.
			sessionId = webLogger.getSessionId();
		}
		
		if(returningBrowserId == null && webLogger.getReturningBrowserId() != null) {
			HttpUtils.addCookie(RETURNING_BROWSER_COOKIE_NAME, "" + webLogger.getReturningBrowserId(), response); // will be recognized the next session it comes back.
			returningBrowserId = webLogger.getReturningBrowserId();
		}
		
	}
	
	
}
